var canshu=1;
var path = window.location.href;
var param = path.split("?");
var paramMap = param[param.length-1].split("=");

var pingtai='30,40'
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}

$("#form-member").validate({
    rules: {
        title:"titleCheck",
        color:"colorCheck",
        lunbotu:"sequenceCheck"
    },
    //提交表单数据
    submitHandler:function(form) {
        //循环class，获取到每个check的选中状态
        if (pingtai=='0'||code=='0'){
            if (pingtai=='0'){
                $("#putong").css('outline','1px solid red')
                $("#qianyue").css('outline','1px solid red')
            }
            if (code==''){
                var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

                $.each(warehouses.data,function(k,s){
                    var warecode= document.getElementById('yun'+s.warehouseCode+'')
                    $(warecode).css('outline','1px solid red')

                })
            }
            layer.msg("请选择配置平台/地域？", {icon: 6, time: 3000});
            return false
        }
        var quzhi=JSON.parse(web.readCookie("loginData"))
        var formData = new FormData();
        formData.append("started_at",$("#logmin").val());
        formData.append("category_id",paramMap[1]);
        formData.append("end_in",$("#logmax").val());
        formData.append("image_url",$("#url").val());
        formData.append("title",$("#biaoti").val());
        formData.append("file",$("#file")[0].files[0]);
        // formData.append("color_value",$("#down_color").val());
        formData.append("sequence",$("#lunbo").val());
        formData.append("created_name",quzhi.user.username);
        formData.append("created_by",quzhi.user.user_ID);
        /*平台和仓库*/
        formData.append("platform",pingtai);
        formData.append("warehouse_code",code);
        formData.append("warehouse_name",CangName);
        console.log($("#file")[0].files[0])
        canshu=canshu+1;
        if (canshu>2){
            return false;
        }else{
            $.ajax({
                //192.168.0.107:7040
                url: root.sroreinter.save_resource_conf,
                cache: false,  //禁用缓存
                type: "POST",
                // 告诉jQuery不要去处理发送的数据
                processData : false,
                // 告诉jQuery不要去设置Content-Type请求头
                contentType : false,
                data: formData,
                success: function (data) {
                    console.log(data)
                    if (data.errorCode=="92"){
                        layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
                    }
                    if (data.errorCode=="94"){
                        layer.msg(data.errorMessage, {icon: 6, time: 3000});
                    }
                    if(data.errorCode=="0") {
                        layer.msg('操作成功!', {icon: 6, time: 1000});
                        setTimeout( function () {
                            var index = parent.location.reload();
                            parent.layer.close(index);
                        },700);
                    }
                    if (data.errorCode=="93"){
                        layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                        return
                    }
                }
            });
        }
    }
})

function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')

    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        pingtai='30'+","+'40'
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        pingtai='30'
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        pingtai='40'
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        pingtai='0'
    }
}
function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('yun'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('yun'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('yun'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }

    code=warcode
    CangName=warcodename
}
function changImg(e){
    var image = new Image();
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 6, time: 2000});
            $("#file").val("")
            continue;//不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {
            image.src =e.target.result ;
            image.onload = function() {
                if(file.size>1*1024*1024){
                    layer.msg('图片不得大于1M!', {icon: 6, time: 2000});
                    $("#file").val("")
                    return;
                }else{
                    if (this.width!=750||this.height!=400){
                        layer.msg('图片大小限制为750*400!', {icon: 6, time: 2000});
                        r=1
                        $("#file").val("")
                        return;
                    } else {
                        $("#myImg").css({width:this.width/5+"px",height:this.height/5+"px",cursor:"pointer"});
                        $("#myImg").attr("src",e.target.result);
                    }
                }
            };
        }
    }
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
$('#pic').click(function (event) {
    event.preventDefault()
    $('#file').click()
})